package it.mp.multitask.utility;

import java.io.File;
import java.io.FilenameFilter;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Bitmap.Config;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import it.mp.multitask.R;
import it.mp.multitask.factory.Style;

public class PatternEntry {
	private PatternEntry() {

	}

	public static final int decodePattern(String pattern, int layoutStandard) {
		if (layoutStandard == 0) {
			return decodePatternStandard(pattern);
		} else if (layoutStandard == 1) {
			return decodePatternDialog(pattern);
		} else if (layoutStandard == 2) {
			return decodePatternIphone(pattern);
		} else {
			return decodePatternStandard(pattern);
		}
	}
	private static final int decodePatternIphone(String pattern) {
		if (pattern.equals("pattern_1")) {
			return R.drawable.scrivania1;
		} else if (pattern.equals("pattern_2")) {
			return R.drawable.scrivania2;
		} else if (pattern.equals("pattern_3")) {
			return R.drawable.scrivania3;
		} else if (pattern.equals("pattern_4")) {
			return R.drawable.scrivania4;
		} else if (pattern.equals("pattern_5")) {
			return R.drawable.scrivania5;
		} else if (pattern.equals("pattern_6")) {
			return R.drawable.scrivania6;
		} else if (pattern.equals("pattern_7")) {
			return R.drawable.scrivania7;
		} else if (pattern.equals("transparent")) {
			return android.R.color.transparent;
		} else {
			return -1;
		}
	}
	private static final int decodePatternStandard(String pattern) {
		if (pattern.equals("pattern_1")) {
			return R.drawable.back;
		} else if (pattern.equals("pattern_2")) {
			return R.drawable.pattern2;
		} else if (pattern.equals("pattern_3")) {
			return R.drawable.pattern3;
		} else if (pattern.equals("pattern_4")) {
			return R.drawable.pattern4;
		} else if (pattern.equals("pattern_5")) {
			return R.drawable.pattern5;
		} else if (pattern.equals("pattern_6")) {
			return R.drawable.pattern6;
		} else if (pattern.equals("pattern_7")) {
			return R.drawable.pattern7;
		} else if (pattern.equals("pattern_8")) {
			return R.drawable.pattern8;
		} else if (pattern.equals("pattern_9")) {
			return R.drawable.pattern9;
		} else if (pattern.equals("pattern_10")) {
			return R.drawable.pattern10;
		} else if (pattern.equals("pattern_11")) {
			return R.drawable.pattern11;
		} else if (pattern.equals("pattern_12")) {
			return R.drawable.pattern12;
		} else if (pattern.equals("pattern_13")) {
			return R.drawable.pattern13;
		} else if (pattern.equals("pattern_14")) {
			return R.drawable.pattern14;
		} else if (pattern.equals("pattern_15")) {
			return R.drawable.pattern15;
		} else if (pattern.equals("pattern_16")) {
			return R.drawable.pattern16;
		} else if (pattern.equals("pattern_17")) {
			return R.drawable.pattern17;
		} else if (pattern.equals("pattern_18")) {
			return R.drawable.pattern18;
		} else if (pattern.equals("pattern_19")) {
			return R.drawable.pattern19;
		} else if (pattern.equals("pattern_20")) {
			return R.drawable.pattern20;
		} else if (pattern.equals("pattern_21")) {
			return R.drawable.pattern21;
		} else if (pattern.equals("pattern_22")) {
			return R.drawable.pattern22;
		} else if (pattern.equals("pattern_23")) {
			return R.drawable.pattern23;
		} else if (pattern.equals("pattern_24")) {
			return R.drawable.pattern24;
		} else if (pattern.equals("pattern_25")) {
			return R.drawable.pattern25;
		} else if (pattern.equals("pattern_26")) {
			return R.drawable.pattern26;
		} else if (pattern.equals("pattern_27")) {
			return R.drawable.pattern27;
		} else if (pattern.equals("pattern_28")) {
			return R.drawable.pattern28;
		} else if (pattern.equals("pattern_29")) {
			return R.drawable.pattern29;
		} else if (pattern.equals("pattern_30")) {
			return R.drawable.pattern30;
		} else if (pattern.equals("transparent")) {
			return android.R.color.transparent;
		} else if (pattern.equals("darker_gray")) {
			return android.R.color.darker_gray;
		} else if (pattern.equals("darker_gray")) {
			return android.R.color.darker_gray;
		} else if (pattern.equals("gray")) {
			return R.color.gray;
		} else if (pattern.equals("gradient")) {
			return R.drawable.gradient;
		} else if (pattern.equals("white")) {
			return android.R.color.white;
		} else if (pattern.equals("black")) {
			return android.R.color.black;
		} else {
			return R.drawable.back;
		}
	}
	private static final int decodePatternDialog(String pattern) {
		if (pattern.equals("pattern_1")) {
			return R.drawable.background_shape;
		} else if (pattern.equals("pattern_2")) {
			return R.drawable.background_shape_white;
		} else if (pattern.equals("pattern_3")) {
			return R.drawable.background_shape_blu;
		} else if (pattern.equals("pattern_4")) {
			return R.drawable.background_shape_gray;
		} else if (pattern.equals("pattern_5")) {
			return R.drawable.background_shape_green;
		} else if (pattern.equals("pattern_6")) {
			return R.drawable.background_shape_red;
		} else if (pattern.equals("pattern_7")) {
			return R.drawable.background_shape_yellow;
		} else {
			return R.drawable.background_shape;
		}

	}

	public static final String[] getChiavi(int layoutStandard) {
		if (layoutStandard == 0) {
			return new String[]{"pattern_1", "pattern_2", "pattern_3", "pattern_4", "pattern_5", "pattern_6", "pattern_7", "pattern_8", "pattern_9",
					"pattern_10", "pattern_11", "pattern_12", "pattern_13", "pattern_14", "pattern_15", "pattern_16", "pattern_17", "pattern_18", "pattern_19",
					"pattern_20", "pattern_21", "pattern_22", "pattern_23", "pattern_24", "pattern_25", "pattern_26", "pattern_27", "transparent",
					"darker_gray", "gray", "gradient", "white", "black", "pattern_28", "pattern_29", "pattern_30"};
		} else if (layoutStandard == 1) {
			return new String[]{"pattern_1", "pattern_2", "pattern_3", "pattern_4", "pattern_5", "pattern_6", "pattern_7"};
		} else if (layoutStandard == 2) {

			String[] chiaviFisse = new String[]{"pattern_1", "pattern_2", "pattern_3", "pattern_4", "pattern_5", "pattern_6", "pattern_7", "transparent"};
			String[] chiaviVariabili = findImageOnSD();

			String[] chiavi = new String[chiaviFisse.length + chiaviVariabili.length];
			System.arraycopy(chiaviFisse, 0, chiavi, 0, chiaviFisse.length);
			System.arraycopy(chiaviVariabili, 0, chiavi, chiaviFisse.length, chiaviVariabili.length);

			return chiavi;
		} else {
			return new String[]{"pattern_1", "pattern_2", "pattern_3", "pattern_4", "pattern_5", "pattern_6", "pattern_7", "pattern_8", "pattern_9",
					"pattern_10", "pattern_11", "pattern_12", "pattern_13", "pattern_14", "pattern_15", "pattern_16", "pattern_17", "pattern_18", "pattern_19",
					"pattern_20", "pattern_21", "pattern_22", "pattern_23", "pattern_24", "pattern_25", "pattern_26", "pattern_27", "transparent",
					"darker_gray", "gray", "gradient", "white", "black", "pattern_28", "pattern_29", "pattern_30"};
		}
	}
	public static final String[] getValori(int layoutStandard) {
		if (layoutStandard == 0) {
			return new String[]{"Pattern 1", "Pattern 2", "Pattern3", "Pattern 4", "Pattern 5", "Pattern 6", "Pattern 7", "Pattern 8", "Pattern 9",
					"Pattern 10", "Pattern 11", "Pattern 12", "Pattern 13", "Pattern 14", "Pattern 15", "Pattern 16", "Pattern 17", "Pattern 18", "Pattern 19",
					"Pattern 20", "Pattern 21", "Pattern 22", "Pattern 23", "Pattern 24", "Pattern 25", "Pattern 26", "Pattern 27", "Transparent", "Dark Gray",
					"Gray", "gradient", "white", "black", "Pattern 28", "Pattern 29", "Pattern 30"};
		} else if (layoutStandard == 1) {
			return new String[]{"Pattern 1", "Pattern 2", "Pattern3", "Pattern 4", "Pattern 5", "Pattern 6", "Pattern 7"};
		} else if (layoutStandard == 2) {
			String[] valoriFisse = new String[]{"Pattern 1", "Pattern 2", "Pattern3", "Pattern 4", "Pattern 5", "Pattern 6", "Pattern 7", "Transparent"};
			String[] valoriVariabili = findImageOnSD();

			String[] valori = new String[valoriFisse.length + valoriVariabili.length];
			System.arraycopy(valoriFisse, 0, valori, 0, valoriFisse.length);
			System.arraycopy(valoriVariabili, 0, valori, valoriFisse.length, valoriVariabili.length);

			return valori;
		} else {
			return new String[]{"Pattern 1", "Pattern 2", "Pattern3", "Pattern 4", "Pattern 5", "Pattern 6", "Pattern 7", "Pattern 8", "Pattern 9",
					"Pattern 10", "Pattern 11", "Pattern 12", "Pattern 13", "Pattern 14", "Pattern 15", "Pattern 16", "Pattern 17", "Pattern 18", "Pattern 19",
					"Pattern 20", "Pattern 21", "Pattern 22", "Pattern 23", "Pattern 24", "Pattern 25", "Pattern 26", "Pattern 27", "Transparent", "Dark Gray",
					"Gray", "gradient", "white", "black", "Pattern 28", "Pattern 29", "Pattern 30"};
		}
	}
	public static final String primoValore() {
		return "pattern_1";
	}

	public static BitmapDrawable createDrawable(Resources res, String pattern, int layoutStandard, int maxHeight, int iconSize) {

		int resID = -1;
		resID = decodePatternIphone(pattern);
		BitmapDrawable bd = null;
		if (resID == -1) {
			bd = new BitmapDrawable(pattern);
		} else {
			bd = createDrawable(res, resID, layoutStandard);
		}
		iconSize = iconSize / 2;
		if ((bd != null) && (bd.getBitmap() != null) && (maxHeight - iconSize)>0) {

			Bitmap originalBitmap = bd.getBitmap();
			int maxWidth = originalBitmap.getWidth();
			int heightBitmap = originalBitmap.getHeight();

			Bitmap cutterImage = Bitmap.createBitmap(originalBitmap, 0, 0, maxWidth, maxHeight - iconSize);

			Bitmap reflectionImage = null;

			Matrix matrix = new Matrix();
			matrix.preScale(1, -1);
			if (maxHeight > heightBitmap) {
				reflectionImage = Bitmap.createBitmap(originalBitmap, 0, heightBitmap - iconSize, maxWidth, iconSize, matrix, false);
			} else {
				if ((maxHeight - (iconSize + iconSize)) > 0) {
					reflectionImage = Bitmap.createBitmap(originalBitmap, 0, maxHeight - (iconSize + iconSize), maxWidth, iconSize, matrix, false);
				} else {
					reflectionImage = Bitmap.createBitmap(originalBitmap, 0, 0, maxWidth, iconSize, matrix, false);
				}
			}

			// Create a new bitmap with same width but taller to fit reflection
			Bitmap bitmapWithReflection = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888);

			// Create a new Canvas with the bitmap that's big enough for
			// the image plus gap plus reflection
			Canvas canvas = new Canvas(bitmapWithReflection);
			// Draw in the original image
			canvas.drawBitmap(cutterImage, 0, 0, null);

			// Draw in the reflection
			canvas.drawBitmap(reflectionImage, 0, maxHeight - iconSize, null);

			// Draw a rectangle using the paint with our linear gradient
			Paint paintShader = new Paint();
			RadialGradient shader = new RadialGradient(0, 0, maxWidth, 0xA4636263, 0xA4c0c0c0, TileMode.CLAMP);
			paintShader.setShader(shader);

			canvas.drawRect(0, maxHeight - iconSize, bitmapWithReflection.getWidth(), bitmapWithReflection.getHeight(), paintShader);

			bd = new BitmapDrawable(bitmapWithReflection);			
			bd.setTileModeX(TileMode.CLAMP);
			bd.setTileModeY(TileMode.CLAMP);

		}
		return bd;
	}
	public static BitmapDrawable createDrawable(Resources res, String pattern) {
		int resID = -1;
		resID = decodePatternIphone(pattern);
		BitmapDrawable bd = null;
		if (resID == -1) {
			bd = new BitmapDrawable(pattern);
		} else {
			bd = createDrawable(res, resID, Style.LAYOUT_EXTENDED);
		}
		return bd;
	}
	public static BitmapDrawable createDrawable(Resources res, int pattern, int layoutStandard) {
		BitmapDrawable bd = null;
		if (layoutStandard == 0) {
			switch (pattern) {
				case android.R.color.transparent :
				case android.R.color.darker_gray :
				case R.color.gray :
				case android.R.color.white :
				case android.R.color.black :
					bd = null;
					break;
				default :
					bd = new BitmapDrawable(BitmapFactory.decodeResource(res, pattern));
					bd.setTileModeX(TileMode.REPEAT);
					bd.setTileModeY(TileMode.REPEAT);
					break;
			}
		} else if (layoutStandard == 1) {
			bd = null;
		} else if (layoutStandard == 2) {
			switch (pattern) {
				case android.R.color.transparent :
				case R.drawable.scrivania1 :
				case R.drawable.scrivania2 :
				case R.drawable.scrivania3 :
					bd = null;
					break;
				default :
					bd = new BitmapDrawable(BitmapFactory.decodeResource(res, pattern));
					bd.setTileModeX(TileMode.CLAMP);
					bd.setTileModeY(TileMode.CLAMP);
					break;
			}
		} else {
			bd = null;
		}
		return bd;
	}

	private static String[] findImageOnSD() {
		File images = new File("/sdcard/mtm");
		File[] imagelist = images.listFiles(new FilenameFilter() {

			public boolean accept(File dir, String name) {
				return ((name.endsWith(".jpg")) || (name.endsWith(".png")));
			}
		});
		String[] urls;
		if (imagelist == null) {
			urls = new String[0];
		} else {
			String[] files = new String[imagelist.length];
			int len = imagelist.length;
			for (int i = 0; i < len; i++) {
				files[i] = imagelist[i].getAbsolutePath();
			}
			urls = new String[files.length];
			len = files.length;
			for (int i = 0; i < len; i++) {
				urls[i] = Uri.parse(files[i]).toString();
			}
		}
		return urls;
	}
}
